package com.bidevalution.support.compare;

import lombok.extern.log4j.Log4j2;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 句子分词实现类
 */
@Log4j2
public class StringHandler {

	/**
	 * input:str
	 * 将字符串分词转换成数组
	 */
	public List<String> stringToArray(String str) {
		
		List<String> list = new ArrayList<String>();
		byte[] bt = str.getBytes();
		InputStream is = new ByteArrayInputStream(bt);
		Reader read = new InputStreamReader(is);
		IKSegmenter iks = new IKSegmenter(read, true);
		try {
			Lexeme t;
			while ((t = iks.next()) != null) {
				list.add(t.getLexemeText());
			}
		} catch (IOException e) {
            log.error(e.getMessage(), e);
		}
		return list;
		
	}

	/**
	 * input:str
	 * 将字符串分词转换成数组
	 */
	public List<List<String>> stringListToArrayList(List<String> list) {
		List<List<String>> wordList = new ArrayList<>();
		for(String str : list){
			wordList.add(stringToArray(str));
		}
		return wordList;

	}

}
